package topo.utils;

import com.zhtelecom.common.base.MyException;
import com.zhtelecom.common.base.MyLog;
import com.zhtelecom.common.snmp.SNMPAPI;
import com.zhtelecom.common.snmp.SNMPFactory;
import com.zhtelecom.common.snmp.SNMPTarget;
import topo.data.NodeDevice;

public class SNMPTargetTool {
    public static SNMPTarget nodeToSNMPConnInfo(NodeDevice node) {
        SNMPTarget snmpConninfo = new SNMPTarget();
        snmpConninfo.nodeIP = node.getIpAddress();
        snmpConninfo.readCommunity = node.getReadCommit();
        snmpConninfo.writeCommunity = node.getWriteCommit();
        snmpConninfo.port = node.getSnmpPort();
        snmpConninfo.snmpVersion = node.getSnmpVersion();
        if (node.getIsEnableSNMPProxy() == 1) {
            snmpConninfo.proxySNMPGateIP = node.getSnmpProxyIP();
        }

        return snmpConninfo;
    }

    public static String getSNMPOIDValue(NodeDevice node, String oid) throws MyException {
        try {
            SNMPTarget snmpConninfo = nodeToSNMPConnInfo(node);
            return SNMPFactory.getSNMPAPI().getOIDValue(oid, snmpConninfo);
        } catch (Exception ex) {
            throw new MyException(ex);
        }
    }

    public static String getSNMPNextOIDValue(NodeDevice node, String oid) throws MyException {
        try {
            SNMPTarget snmpConninfo = nodeToSNMPConnInfo(node);
            return SNMPFactory.getSNMPAPI().getNextOIDValue(oid, snmpConninfo);
        } catch (Exception ex) {
            throw new MyException(ex);
        }
    }

    public static String checkAgentErroMsg(NodeDevice node) {
        try {
            SNMPTarget snmpConninfo = nodeToSNMPConnInfo(node);
            SNMPAPI snmpAPI = SNMPFactory.getSNMPAPI();
            snmpAPI.checkSnmpAgentActive(snmpConninfo);
            return null;
        } catch (Exception ex) {
            MyLog.err("检查SNMPAgent是否活动，发生错误，设备ID:" + node.getNodeName() + ",错误:" + ex.getMessage());
            String msg = ex.getMessage();
            if (msg == null)
                ;
            return ex.toString();
        }
    }
}